#include<iostream> 
using namespace std;
int main(){
	
	int n;
	cin >> n;
	
	char * c_array = new char[n];
	int * c2_array = new int[n];
	int * i_array = new int[n];   //i array is use save the input.
	int * s_array = new int[n];   //s array is use for s[i].
	int * m_array = new int[n];  //m array is use for count the matches of node.
	int * v_array = new int[n];  //v array is use for count of '(' -1  ')' +1.	
	
	for (int i=0;i<n;i++) cin >> c_array[i];
	for (int i=1;i<n;i++) cin >> i_array[i];
		
	i_array[0]=0;	
	
	for (int i=0;i<n;i++) {
		
		i_array[i]--;
		
		m_array[i]=0;
		v_array[i]=0;
		
		if (c_array[i]=='('){
			c2_array[i]=-1;
		}else{
			c2_array[i]=1;
		}
	}
	
	m_array[0]=0;
	v_array[0]=c2_array[0];
	
	for(int i=1;i<n;i++){
		int father_m=m_array[i_array[i]];
		int father_v=v_array[i_array[i]];
		if(c2_array[i]*father_v>=0){
			m_array[i]=father_m;
			v_array[i]=father_v+c2_array[i];
		}else{
			m_array[i]=father_m+1;
			if(father_v>0){
				v_array[i]=father_v-1;
			}else{
				v_array[i]=father_v+1;				
			}			
		}
		cout<<i<<":"<<m_array[i]<<":"<<v_array[i]<<endl;
	}
	cout<<"test first.";		
	int result=0;

	cout<<result<<":"<<0<<":"<<5<<":"<<m_array[0]<<endl;
	for(int i=1;i<n;i++){
		cout<<result<<":"<<(i+1)<<":"<<n<<":"<<m_array[i]<<endl;
		result= result ^ ((i+1)*m_array[i]);
	}
	
	cout<<result<<endl;
	cout<<"test after.";
	
	return 0; 
}
